package com.jnu.book;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class BookListMainActivity extends AppCompatActivity
{

    private RecyclerView mainRecyclerView;
    private List<book> books;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        mainRecyclerView = (RecyclerView) findViewById(R.id.recycle_view_books);
        mainRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mainRecyclerView.setAdapter(new MyRecyclerViewAdapter(getBooks()));
    }

    protected void initData()
    {
        setBooks(new ArrayList<book>());
        getBooks().add(new book("软件项目管理案例教程（第4版）", R.drawable.book_2));
        getBooks().add(new book("创新工程实践", R.drawable.book_no_name));
        getBooks().add(new book("信息安全数学基础（第2版）", R.drawable.book_1));
    }

    public List<book> getBooks() {
        return books;
    }

    public void setBooks(List<book> books) {
        this.books = books;
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter{
        private List<book> books;

        public MyRecyclerViewAdapter(List<book> books) {
            this.books=books;
        }
        @NonNull
        //加载视图
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.book_item_holder, parent, false);
                return new MyViewHolder(view);


        }
        //绑定数据
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, int position) {

                MyViewHolder holder = (MyViewHolder) Holder;
                holder.getImageView().setImageResource(books.get(position).getCoverResourceId());
                holder.getTextView().setText(books.get(position).getTitle());


        }

        @Override
        public int getItemCount() {
            return books.size();
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final TextView textView;

        public MyViewHolder(View view) {
            super(view);

            this.imageView=view.findViewById(R.id.image_view_book_cover);
            this.textView=view.findViewById(R.id.text_view_book_title);
        }

        public ImageView getImageView() {
            return imageView;
        }

        public TextView getTextView() {
            return textView;
        }
    }

}